package com.teddy.DTXMB.Controller;

import java.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.teddy.DTXMB.Model.Product;
import com.teddy.DTXMB.Service.ProductService;

@Controller
public class ProductController {
	
	private static final Logger logger = LoggerFactory.getLogger(ProductController.class);
	
	@Autowired
	private ProductService productService;
	
	@RequestMapping(value = "/product/show", method = RequestMethod.GET)
	
	public String ShowProdcut (Model model){
		// @RequestParam(value="id", required=false, defaultValue="1")int id
		// Show Product Method		
		logger.info("Show Product Method Called!");
		logger.info("Init ProductService...");
		
		if(productService !=null){
			logger.info("Init ProductService Done...");
		}
		else{
			logger.error("Init ProductService Fail...");
		}
		
		List<Product> products = productService.getAllProduct();
        model.addAttribute("products", products);
 
		return "products";
	}
	
	@RequestMapping(value = "/product/show/{productid}", method = RequestMethod.GET)
	public String ShowSingleProduct(@PathVariable(value="productid") int productID, Model model){
		Product product = productService.getProductByID(productID);
		model.addAttribute("product", product);
		return "product";
	}
}
